package com.aaa;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;

@SpringBootApplication
public class RootApplication {

    private static final Logger logger = LoggerFactory.getLogger(RootApplication.class);

    public static void main(String[] args) throws IOException {

        /*
        在 Spring Boot 中，通常每个类（包括 Controller、Service、Repository 等）
        都会使用独立的日志记录器对象来记录日志。这样做的好处是能够清晰地跟踪日志的来源，方便排查问题和调试。
        虽然看起来可能会比较繁琐，但这种做法是比较标准和推荐的。
        */



        ConfigurableApplicationContext application = SpringApplication.run(RootApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String datasource_url = env.getProperty("spring.datasource.url");
        String property = env.getProperty("server.servlet.context-path");
        String path = property == null ? "" : property;
        System.out.println(
                "\n\t" +
                        "----------------------------------------------------------\n\t" +
                        "datasource_url = " + datasource_url + "\n\t" +
                        "Application Sailrui-Boot is running! Access URLs:\n\t" +
                        "Local: \t\thttp://localhost:" + port + path + "/\n\t\n\t" +
                        "External: \thttp://" + ip + ":" + port + path + "/\n\t\n\t" +
                        "------------------------------------------------------------");
        System.out.println("记得打开 phpstudy 内置的MySQL 服务器, 不然web应用没办法正常访问...");
        System.out.println(" 需要清除上一个 SpringApplication.run .....");





        Constants constants = new Constants();
        // 文件路径
        String filePath = constants.getSETTING_FILE_PATH();
        // 创建文件对象
        File file = new File(filePath);
        // 获取文件所在的文件夹路径
        String directoryPath = file.getParent();
        // 创建文件夹
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            if (directory.mkdirs()) {
                System.out.println("文件夹已创建：" + directoryPath);
            } else {
                System.out.println("无法创建文件夹：" + directoryPath);
                return;
            }
        }

        // 检查文件是否存在
        if (!file.exists()) {
            try {
                // 如果文件不存在，则创建文件
                if (file.createNewFile()) {
                    System.out.println("文件已创建：" + filePath);
                } else {
                    System.out.println("无法创建文件：" + filePath);
                }
            } catch (IOException e) {
                System.out.println("创建文件时出现异常：" + e.getMessage());
            }
        } else {
            System.out.println("文件已存在，无需创建：" + filePath);

            try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}






